#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStandardItemModel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  connect(&m_Tcpclient, SIGNAL(TcpMsg(QString)), this,
          SLOT(showReveiveMsg(QString)));
}

MainWindow::~MainWindow() { delete ui; }

void MainWindow::on_pushButton_clicked() {
  m_Tcpclient.connectToServer();
  qDebug() << m_Tcpclient.isValid() << endl;

  if (m_Tcpclient.isValid() &&
      m_Tcpclient.state() == QAbstractSocket::ConnectedState) {
    qDebug() << "connect ok" << endl;
  } else {
    qDebug() << "connect fail" << endl;
  }
}

void MainWindow::on_btn_send_clicked() {

  QString writeText = ui->lineEdit->text();
  m_Tcpclient.sendData(writeText);
}

void MainWindow::showReveiveMsg(const QString &msg) {
  ui->plainTextEdit->appendPlainText(msg);
}
